﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.PO;

namespace Com.CMS263.DAO
{
    public class GoodsDAO:BaseService
    {
        IList<Good> goodList = new List<Good>();
        public IList<Good> FinlAll()
        {
            string hql = "from Good order by AddDate desc";
            return FindList<Good>(hql, null, null, null);
        }
        public IList<Good> FindListByKey(int pagesize, int pageindex)
        {
            try
            {
                string hql = "from Good";
                return DBHelper.ExecuteHQL(hql + "  order by AddDate desc").SetFirstResult((pageindex - 1) * pagesize).SetMaxResults(pagesize).List<Good>();
            }
            catch { return null; }
        }

        #region 根据品牌得到信息条数
        /// <summary>
        /// 根据品牌得到信息条数
        /// </summary>
        /// <param name="brandid"></param>
        /// <returns></returns>
        public int GetCountByBrandId(string brandid)
        {
            string hql = "select count(*) from Good where BrandId in (" + brandid + ")";
            return Convert.ToInt32(GetValue(hql));
        }
        #endregion

        public IList<Good> FindListByKey(string name, string isSale, string isDelete, string isBest, string isNew, int pageindex, int pagesize)
        {
            string hql = "from Good where 1=1 ";
            #region //查询条件
            if (!string.IsNullOrEmpty(name))
            {
                hql += " and GoodsName like '%"+name+"%'";
            }
             if (!string.IsNullOrEmpty(isSale))
            {
                if (isSale.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsSale='" + isSale + "'";
                }
            }
             if (!string.IsNullOrEmpty(isDelete))
            {
                 if (isDelete.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsDelete='" + isDelete + "'";
                }
            }
             if (!string.IsNullOrEmpty(isBest))
            {
                if (isBest.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsBest='" + isBest + "'";
                }
            }
             if (!string.IsNullOrEmpty(isNew))
            {
                if (isNew.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsNew='" + isNew + "'";
                }
            }
            #endregion
            hql += "  order by AddDate desc";
            goodList = FindList<Good>(hql, null, (pageindex-1)*pagesize, pagesize);
            return goodList;
        }

        public IList<Good> FindListByKey(string name, string isSale, string isDelete, string isBest, string isNew)
        {
            string hql = "from Good where 1=1 ";
            #region //查询条件
            if (!string.IsNullOrEmpty(name))
            {
                hql += " and GoodsName like '%" + name + "%'";
            }
            if (!string.IsNullOrEmpty(isSale))
            {
                if (isSale.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsSale='" + isSale + "'";
                }
            }
            if (!string.IsNullOrEmpty(isDelete))
            {
                if (isDelete.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsDelete='" + isDelete + "'";
                }
            }
            if (!string.IsNullOrEmpty(isBest))
            {
                if (isBest.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsBest='" + isBest + "'";
                }
            }
            else if (!string.IsNullOrEmpty(isNew))
            {
                if (isNew.Equals("all"))
                {
                    hql += " ";
                }
                else
                {
                    hql += " and IsNew='" + isNew + "'";
                }
            }
            #endregion
            hql += "  order by AddDate desc";
            goodList = FindList<Good>(hql, null, null, null);
            return goodList;
        }
    }
}
